package com.jerry.template.module.mianpage.shouye.recommend.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jerry.template.R;
import com.jerry.template.module.mianpage.shouye.recommend.home.entity.TestDetail;
import com.jerry.template.views.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 16-5-9.
 */
public class RecycleViewAdapter extends RecyclerView.Adapter {
    private  Context context;

    public static interface OnRecyclerViewListener {
        void onItemClick(int position,int viewId);

        boolean onItemLongClick(int position);
    }


    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private static final String TAG = "RecommendConnection";
    private List<TestDetail.DataBean.DataBean1> list;

    public RecycleViewAdapter(Context context, List<TestDetail.DataBean.DataBean1> list) {
        this.context=context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
//        Log.d(TAG, "onCreateViewHolder, i: " + i);
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recommend_list_item, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new PersonViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
//        Log.d(TAG, "onBindViewHolder, i: " + i + ", viewHolder: " + viewHolder);

        //控件都放到ViewHolder里面
        PersonViewHolder holder = (PersonViewHolder) viewHolder;
        holder.position = i;

        //获取数据
        TestDetail.DataBean.DataBean1 conntent = list.get(i);

        //控件显示对应数据
        if(conntent!=null && conntent.getGroup()!=null && conntent.getGroup().getUser()!=null){
            holder.title.setText(conntent.getGroup().getUser().getName());
            Glide.with(context).load(conntent.getGroup().getUser().getAvatar_url()).into(holder.userHead);

            holder.duanzi_content.setText(conntent.getGroup().getText());

            holder.duanzi_Tag.setText(conntent.getGroup().getCategory_name());

            if(conntent.getGroup().getLarge_image()==null){
                holder.content_imgOrGifOrVideo.setImageDrawable(context.getResources().getDrawable(R.drawable.icon));
            }else {

                Glide.
                        with(context).
                        load(conntent.getGroup().getLarge_image().getUrl_list().get(0).getUrl()).
                        into(holder.content_imgOrGifOrVideo);
            }

            holder.zanYiGe.setText(conntent.getGroup().getDigg_count()+"");
            holder.unUseless.setText(conntent.getGroup().getBury_count()+"");
            holder.commentNum.setText(conntent.getGroup().getComment_count()+"");
            holder.shareNum.setText(conntent.getGroup().getShare_count()+"");
        }

    }

    @Override
    public int getItemCount() {

        return list!=null?list.size():0;

    }

    class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        @BindView(R.id.user_head)
        public CircleImageView userHead;

        @BindView(R.id.title)
        public TextView title;

        @BindView(R.id.content)
        public TextView duanzi_content;

        @BindView(R.id.duanzi_Tag)
        public Button duanzi_Tag;

        @BindView(R.id.content_imgOrGifOrVideo)
        public ImageView content_imgOrGifOrVideo;

        @BindView(R.id.zanYiGe)
        public Button zanYiGe;

        @BindView(R.id.unUseless)
        public Button unUseless;

        @BindView(R.id.commentNum)
        public Button commentNum;

        @BindView(R.id.shareNum)
        public Button shareNum;


        public int position;

        public PersonViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        @Override
        @OnClick({R.id.user_head
                ,R.id.title
                ,R.id.content
                ,R.id.duanzi_Tag
                ,R.id.content_imgOrGifOrVideo
                ,R.id.zanYiGe
                ,R.id.unUseless
                ,R.id.commentNum
                ,R.id.shareNum})
        public void onClick(View v) {
            int viewId = v.getId();
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(position,viewId);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {

            }
            return false;
        }
    }
}
